package org.nbict.iot.task.once;

import org.nbict.iot.Pair;
import org.nbict.iot.persist.repo.RedisRepo;
import org.nbict.iot.task.once.charge.ChargeStatus;
import org.nbict.iot.task.once.journey.JourneyStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * 一段行程
 * Created by songseven on 18/6/22.
 */
public class TimeSpanWindow {

    private Pointer start; //行程起始点
    private Pointer end;  //行程结束点

    private Pointer current; //当前点
    private String vin;

    private List<Pair<Pointer, Pointer>> holder; //记录行程点

    private List<Pair<Pointer, Pointer>> chargeHolder; //记录充电点

    private Pair<Pointer, Pointer> remind; //记录连续间隔<30s的点

    private List<Pair<Long, Long>> remindHolder;

    private Pointer charging; //开始充电点

    public TimeSpanWindow(String vin, RedisRepo repo) {
        this.setCurrent(new Pointer(0l, JourneyStatus.END)); //默认熄火
        this.vin = vin;
        this.holder = new ArrayList<>();
        this.chargeHolder = new ArrayList<>();
        this.remind = new Pair<Pointer, Pointer>();

        this.remindHolder = new ArrayList<>();

//        this.repo = repo;
//        preSpanEndTimestamp = 0l;
    }

    public TimeSpanWindow(String vin) {
        this(vin, null);
    }

    public Pointer start() {
        return start;
    }

    public Pointer end() {
        return end;
    }

    /**
     * 旅程开始
     */
    public TimeSpanWindow start(Pointer start) {
        //stop = false;
        this.start = start;
        this.current.setCurrentTime(start.currentTime());
        this.current.setCurrentStatus(JourneyStatus.START);
        this.current.setCharged(ChargeStatus.UNCHARGED); //!important

        //remove before
//        if (!isDump) {
//            repo.removeWithinTimestamp(vin, preSpanEndTimestamp, start.currentTime());
//        }
        return this;
    }

    public void setStart(Pointer start) {
        this.start = start;
    }

    public void setEnd(Pointer end) {
        this.end = end;
    }

    /**
     * 旅程结束
     */
    public void stop(Pointer end) {
        this.end = end;
        this.current = new Pointer(end.vin(), end.currentTime(),
                JourneyStatus.END, end.getSoc(), end.getLon(), end.getLat(),
                end.charged());

        //记录下这段旅程
        holder.add(new Pair(start, this.end));

//        //remove current
//        if (!isDump) {
//            repo.removeWithinTimestamp(vin, start.currentTime(), end.currentTime());
//        }
//        //dump
//        else if (System.currentTimeMillis() / 1000 - end.currentTime() > delay) {
//            repo.removeTimestamp(vin, start.currentTime(), end.currentTime());
//            if (mongoRepo != null) {
//                TimeSpan span = new TimeSpan(vin,
//                        start.currentTime(), end.currentTime(),
//                        start.getLon(), start.getLat(), end.getLon(), end.getLat(),
//                        end.getSoc() - start.getSoc());
//                //插入mongo
//                mongoRepo.insert("car_run_journey", span.toDocument());
//            }
//        }

//        if (count == 45) {
//            System.out.println();
//        }
//        System.out.println(vin + " : " + count++);
        //重置，以便于计算下一段旅程
        reset();
    }

    public Pointer current() {
        return this.current;
    }

    public void setCurrent(Pointer current) {
        this.current = current;
    }

    /**
     *
     * @param pointer
     */
    public TimeSpanWindow take(Pointer pointer) {

        //跳过登入及登出包
        if (pointer.status() != JourneyStatus.LOGIN
                && pointer.status() != JourneyStatus.LOGOUT) {
            if (this.remind.isEmpty()) { //初始化连续起始点
                this.remind.setLeft(pointer);
            }
            if (this.charging == null && pointer.charged() == ChargeStatus
                    .CHARGING) { //!important
                this.charging = pointer;
            }
            current.status().judge(this, pointer);
        }
        return this;
    }

    public String getVin() {
        return vin;
    }

    public TimeSpanWindow setVin(String vin) {
        this.vin = vin;
        return this;
    }

    public void reset() {
        this.start = null;
    }

    public void setCurrentTime(Pointer pointer) {
        this.current().setCurrentTime(pointer.currentTime()).setCharged(pointer.charged()); //充电状态也记录下
    }

    public List<Pair<Pointer, Pointer>> getHolder() {
        return holder;
    }

    public List <Pair<Pointer, Pointer>>chargeHolder() {
        return this.chargeHolder;
    }

    public Pointer charging() {
        return this.charging;
    }

    public TimeSpanWindow setCharging(Pointer charging) {
        this.charging = charging;
        return this;
    }

    public Pair<Pointer, Pointer> getRemind() {
        return remind;
    }

    public TimeSpanWindow setRemind(Pair<Pointer, Pointer> remind) {
        this.remind = remind;
        return this;
    }

    public List<Pair<Long, Long>> getRemindHolder() {
        return remindHolder;
    }

    public TimeSpanWindow setRemindHolder(List<Pair<Long, Long>> remindHolder) {
        this.remindHolder = remindHolder;
        return this;
    }

}
